package com.quanfaqiang.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.mapper.FurnitureMapper;
import com.quanfaqiang.demo1.mapper.QuyutypesMapper;
import com.quanfaqiang.demo1.mapper.TaskMapper;
import com.quanfaqiang.demo1.pojo.Furniture;
import com.quanfaqiang.demo1.pojo.Quyutypes;
import com.quanfaqiang.demo1.pojo.Task;
import com.quanfaqiang.demo1.service.QuyutypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuyutypesServiceImpl implements QuyutypesService {
    @Autowired
    private QuyutypesMapper quyutypesMapper;
    @Autowired
    private FurnitureMapper furnitureMapper;
    @Autowired
    private TaskMapper taskMapper;


    @Override
    public List<Quyutypes> selectAllType() {
        return quyutypesMapper.selectList(null);
    }

    @Override
    public Page<Quyutypes> selectAll(Integer currentPage, Integer pageSize) {
        Page<Quyutypes> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Quyutypes> queryWrapper = new QueryWrapper<>();
        Page<Quyutypes> TypePage = quyutypesMapper.selectPage(page, queryWrapper);
        System.out.println("当前页:" + TypePage.getCurrent());
        System.out.println("每页记录数:" + TypePage.getSize());
        System.out.println("总记录数:" + TypePage.getTotal());
        System.out.println("总页数:" + TypePage.getPages());
        return TypePage;
    }

    @Override
    public Boolean selectTypeName(String typeName) {
        List<Quyutypes> typeList = quyutypesMapper.selectList(new QueryWrapper<Quyutypes>().eq("name", typeName));
        if (typeList != null && typeList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Page<Quyutypes> serchTypeName(String typeName, Integer currentPage, Integer pageSize) {
        Page<Quyutypes> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Quyutypes> queryWrapper = new QueryWrapper<Quyutypes>().like("name", typeName);
        Page<Quyutypes> TypePage = quyutypesMapper.selectPage(page, queryWrapper);

        return TypePage;
    }

    @Override
    public void update(Quyutypes type) {
        quyutypesMapper.updateById(type);
    }

    @Override
    public void add(Quyutypes type) {
        type.setId(null);
        quyutypesMapper.insert(type);
    }

    @Override
    public void delete(Integer id) {
        List<Furniture> furnitureList = furnitureMapper.selectList(new QueryWrapper<Furniture>().eq("furniture_types_id", id));
        if (furnitureList.size()>0){
            for (Furniture furniture : furnitureList) {
                List<Task> furnitureId = taskMapper.selectList(new QueryWrapper<Task>().eq("furniture_id", furniture.getId()));
                if(furnitureId.size()>0){
                    for (Task task : furnitureId) {
                        taskMapper.deleteById(task);
                    }
                }
                furnitureMapper.deleteById(furniture);
            }

        }
        quyutypesMapper.deleteById(id);
    }

    @Override
    public void deleteIds(List<Integer> ids) {
        for (Integer id : ids) {
            List<Furniture> furnitureList = furnitureMapper.selectList(new QueryWrapper<Furniture>().eq("furniture_types_id", id));
            if (furnitureList.size()>0){
                for (Furniture furniture : furnitureList) {
                    List<Task> furnitureId = taskMapper.selectList(new QueryWrapper<Task>().eq("furniture_id", furniture.getId()));
                    if(furnitureId.size()>0){
                        for (Task task : furnitureId) {
                            taskMapper.deleteById(task);
                        }
                    }
                    furnitureMapper.deleteById(furniture);
                }

            }
            quyutypesMapper.deleteById(id);
        }

    }
}
